package com.tjetc.view;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;

import com.tjetc.domain.*;
import com.tjetc.proxy.ClientProxy;
import com.tjetc.service.OrderService;
import com.tjetc.service.OrderSettlementService;
import com.tjetc.service.VehicleService;
import com.tjetc.vo.AppData;
import com.tjetc.vo.Result;

import java.util.List;
import java.util.Scanner;

public class OrderCustomerView {
    private static Scanner scanner=new Scanner(System.in);
    private OrderService orderService= ClientProxy.getProxyInstance(OrderService.class);
    private OrderSettlementService orderSettlementService=ClientProxy.getProxyInstance(OrderSettlementService.class);
    private VehicleService vehicleService=ClientProxy.getProxyInstance(VehicleService.class);
    public void start() {
        while (true){
            System.out.println("🥰🥰~~~~~~客户订单管理页面~~~~~~🥰🥰");
            System.out.println("1.查看未结算的订单表");
            System.out.println("2.进行订单的结算");
            System.out.println("3.进行取车");
            System.out.println("4.查询已结算的订单");
            System.out.println("5.取消订单");
            System.out.println("0.返回上一级");
            System.out.println("请输入你的选择");
            int i = scanner.nextInt();
            switch (i){
                case 1:
                    list();
                    break;
                case 2:
                    settleAccounts();
                    break;
                case 3:
                    getTheCar();
                    break;
                case 4:
                    list2();
                    break;
                case 5:
                    cancel();
                    break;
                case 0:
                    return;
                default:
                    break;
            }
        }
    }

    private void cancel() {
        List<Order> list=orderService.findList(0,1);
        if (list==null ||list.size()==0){
            System.out.println("可以取消的订单为空!");
        }else {
            for (Order order : list) {
                Customer customer = order.getCustomer();
                CustomerMessage customerMessage = customer.getCustomerMessage();
                Vehicle vehicle = order.getVehicle();
                Integer orderStatus = order.getOrderStatus();
                System.out.println("订单ID:" + order.getOrderId() +
                        ",客户账户:" + customer.getAccount() +
                        ",客户的真实姓名:" + customerMessage.getName() +
                        ",车辆的型号:" + vehicle.getModel() +
                        ",车辆的颜色:" + vehicle.getColor() +
                        ",车牌号:" + vehicle.getLicensePlate() +
                        ",租车开始的日期:" + order.getRentalStartDate() +
                        ",租车预计结束的日期:" + order.getRentalEndDate() +
                        ",订单状态:" + (orderStatus == 0 ? "待审核" : orderStatus == 1 ? "已确认" : orderStatus == 2 ? "进行中" : "已完成") +
                        ",订单预计总费用:" + order.getTotalCost() +
                        ",订单提交日期:" + order.getOrderDate());
            }
            System.out.println("请输入想要取消的订单ID");
            String cancel = scanner.next();
            Result result = orderService.selectById(cancel);
            if (result.getCode()!=500){
                Order order = (Order) result.getData();
                Integer vehicleId = order.getVehicleId();
                Vehicle vehicle=new Vehicle();
                vehicle.setVehicleId(vehicleId);
                vehicle.setCurrentStatus(0);
                vehicleService.update(vehicle);
            }
            Result delete = orderService.delete(cancel);
            System.out.println(delete.getMsg());
        }
    }

    /**
     * 查询所有已结算订单
     */
    private void list2() {
        List<OrderSettlement> list=orderSettlementService.list(AppData.customer.getCustomerId());
        //判断是否为空
        if (list==null ||list.size()==0){
            System.out.println("还没有已完成结算的订单!");
        }else {
            for (OrderSettlement orderSettlement : list) {
                Order order = orderSettlement.getOrder();
                Customer customer = order.getCustomer();
                CustomerMessage customerMessage = customer.getCustomerMessage();
                Vehicle vehicle = order.getVehicle();
                System.out.println("订单ID:"+order.getOrderId()+
                        ",客户的真实姓名:"+customerMessage.getName()+
                        ",车辆的型号:"+vehicle.getModel()+
                        ",车辆的颜色:"+vehicle.getColor()+
                        ",车牌号:"+vehicle.getLicensePlate()+
                        ",租车开始的日期:"+order.getRentalStartDate()+
                        ",租车结束的日期:"+orderSettlement.getActualReturnDate()+
                        ",额外的费用:"+orderSettlement.getAdditionalCharges()+
                        ",订单总费用:"+orderSettlement.getFinalCost()+
                        ",订单结算日期:"+orderSettlement.getSettlementDate());
            }

        }
    }

    /**
     * 进行取车
     */
    private void getTheCar() {
        Customer customer1 = AppData.customer;
        Integer id = customer1.getCustomerId();
        //根据用户id和订单状态查询订单
        List<Order> list = orderService.findByCustomerIdAndStatus(id,1);
        //判断订单是否为空
        if (list==null ||list.size()==0){
            System.out.println("当前的订单为空!");
        }else {
            for (Order order : list) {
                Customer customer = order.getCustomer();
                CustomerMessage customerMessage = customer.getCustomerMessage();
                Vehicle vehicle = order.getVehicle();
                Integer orderStatus = order.getOrderStatus();
                System.out.println("订单ID:"+order.getOrderId()+
                        ",客户账户:"+customer.getAccount()+
                        ",客户的真实姓名:"+customerMessage.getName()+
                        ",车辆的型号:"+vehicle.getModel()+
                        ",车辆的颜色:"+vehicle.getColor()+
                        ",车牌号:"+vehicle.getLicensePlate()+
                        ",租车开始的日期:"+order.getRentalStartDate()+
                        ",租车预计结束的日期:"+order.getRentalEndDate()+
                        ",订单状态:"+(orderStatus==0?"待审核":orderStatus==1?"已确认":orderStatus==2?"进行中":"已完成")+
                        ",订单预计总费用:"+order.getTotalCost()+
                        ",订单提交日期:"+order.getOrderDate());
            }
            System.out.println("请选择想要取车的订单ID");
            String orderId = scanner.next();
            Result result1=orderService.selectById(orderId);
            Order order = (Order) result1.getData();
            //查询订单状态，如果订单状态为已确认，则修改为进行中
            if (order.getOrderStatus()==1){
                //判断当前时间是否在租车开始时间之后，如果在之后，则能够取车
                if(order.getRentalStartDate().isBefore(LocalDateTime.now())){
                    orderService.update(orderId,2);
                    System.out.println("取车成功");
                }else {
                    System.out.println("未到取车时间");
                }
            }else {
                System.out.println("此车辆不可提!");
            }
        }
    }

    /**
     * 结算订单
     */
    private void settleAccounts() {
        Customer customer1 = AppData.customer;
        Integer id = customer1.getCustomerId();
        //根据用户id和订单状态列出所有订单
        List<Order> list = orderService.findByCustomerIdAndStatus(id,2);
        //判断订单是否为空
        if (list==null ||list.size()==0){
            System.out.println("当前的订单为空!");
        }else {
            for (Order order : list) {
                Customer customer = order.getCustomer();
                CustomerMessage customerMessage = customer.getCustomerMessage();
                Vehicle vehicle = order.getVehicle();
                Integer orderStatus = order.getOrderStatus();
                System.out.println("订单ID:"+order.getOrderId()+
                        ",客户账户:"+customer.getAccount()+
                        ",客户的真实姓名:"+customerMessage.getName()+
                        ",车辆的型号:"+vehicle.getModel()+
                        ",车辆的颜色:"+vehicle.getColor()+
                        ",车牌号:"+vehicle.getLicensePlate()+
                        ",租车开始的日期:"+order.getRentalStartDate()+
                        ",租车预计结束的日期:"+order.getRentalEndDate()+
                        ",订单状态:"+(orderStatus==0?"待审核":orderStatus==1?"已确认":orderStatus==2?"进行中":"已完成")+
                        ",订单预计总费用:"+order.getTotalCost()+
                        ",订单提交日期:"+order.getOrderDate());
            }
            System.out.println("请选择想要结算的订单ID");
            String orderId = scanner.next();
            Result result1=orderService.selectById(orderId);
            Order order = (Order) result1.getData();
            //判断订单是否为进行中状态
            if (order.getOrderStatus()==2){
                OrderSettlement orderSettlement = new OrderSettlement();
                orderSettlement.setOrderId(orderId);
                orderSettlement.setActualReturnDate(new Date());
                orderSettlement.setAdditionalCharges(null);
                orderSettlement.setFinalCost(null);
                orderSettlement.setSettlementDate(new Date());
                Result result=orderSettlementService.insert(orderSettlement,order);
                System.out.println(result.getMsg());
                //更新车辆的状态
                Vehicle vehicle = new Vehicle();
                Integer vehicleId = order.getVehicleId();
                vehicle.setVehicleId(vehicleId);
                vehicle.setCurrentStatus(0);
                vehicleService.update(vehicle);
                //更新订单
                orderService.update(orderId,3);
                //显示结算后订单
//                OrderSettlement orderSettlement1=orderSettlementService.findByOrderId(orderId);
            }else {
                System.out.println("此订单无法结算");
            }
        }
    }

    /**
     *  查询用户所有未结算订单
     */
    private void list() {
        Customer customer1 = AppData.customer;
        Integer id = customer1.getCustomerId();
        //根据用户id和订单状态查询所有未结算订单
        List<Order> list = orderService.findByCustomerIdAndWei(id,3);
        //判断所有未结算订单
        if (list==null ||list.size()==0){
            System.out.println("当前的订单为空!");
        }else {
            for (Order order : list) {
                Customer customer = order.getCustomer();
                CustomerMessage customerMessage = customer.getCustomerMessage();
                Vehicle vehicle = order.getVehicle();
                Integer orderStatus = order.getOrderStatus();
                System.out.println("订单ID:"+order.getOrderId()+
                        ",客户账户:"+customer.getAccount()+
                        ",客户的真实姓名:"+customerMessage.getName()+
                        ",车辆的型号:"+vehicle.getModel()+
                        ",车辆的颜色:"+vehicle.getColor()+
                        ",车牌号:"+vehicle.getLicensePlate()+
                        ",租车开始的日期:"+order.getRentalStartDate()+
                        ",租车预计结束的日期:"+order.getRentalEndDate()+
                        ",订单状态:"+(orderStatus==0?"待审核":orderStatus==1?"已确认":orderStatus==2?"进行中":"已完成")+
                        ",订单预计总费用:"+order.getTotalCost()+
                        ",订单提交日期:"+order.getOrderDate());
            }
        }
    }
}
